package com.kelp.codetoolkit.respository

import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.util.xmlb.XmlSerializerUtil
import com.kelp.codetoolkit.persistent.CodeToolkitGlobalSettingState
import com.kelp.codetoolkit.utils.ProjectUtil

/**
 * @author: liuyuebai
 * @date: 2024/12/29 09:34
 * @description:
 */
@Service(Service.Level.PROJECT)
@State(
    name = "CodeToolkitGlobalSettingRepository",
    storages = [Storage("code_toolkit_global.xml")]
)
class CodeToolkitGlobalSettingRepository: PersistentStateComponent<CodeToolkitGlobalSettingState> {
    private val globalSettingState = CodeToolkitGlobalSettingState()
    override fun getState(): CodeToolkitGlobalSettingState {
        return this.globalSettingState
    }

    override fun loadState(state: CodeToolkitGlobalSettingState) {
        // 可以使用Kotlin的copy函数或其他方式进行状态复制
        // 这里假设已经有合适的复制工具函数xmlSerializerUtil
        XmlSerializerUtil.copyBean(state, this.globalSettingState)
    }

    companion object {
        fun getInstance(project: Project): CodeToolkitGlobalSettingRepository{
           return project.getService(CodeToolkitGlobalSettingRepository::class.java)
        }
    }
}